package com.hot.hot_backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hot.hot_backend.pojo.HotOrders;
import com.hot.hot_backend.pojo.HotUser;
import com.hot.hot_backend.pojo.Vo;
import com.hot.hot_backend.service.impl.HotOrdersServiceImpl;
import com.hot.hot_backend.service.impl.HotUserServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  订单
 * </p>
 *
 * @author hot
 * @since 2024-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/hot_orders")
public class HotOrdersController {

    @Resource
    private HotOrdersServiceImpl ordersService;

    @Resource
    private HotUserServiceImpl userService;

    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<HotOrders> page(@PathVariable("current") Integer current) {
        Page<HotOrders> ordersPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<HotOrders> page = ordersService.page(ordersPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有订单！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 搜索查询
    @GetMapping("/search/{current}/{name}")
    public Vo<HotOrders> search(@PathVariable("current") Integer current, @PathVariable("name") String name) {
        LambdaQueryWrapper<HotOrders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),HotOrders::getGoodsName, name);
        Page<HotOrders> ordersPage = new Page<>(current <= 0 ? 1 : current, 6);
        Page<HotOrders> page = ordersService.page(ordersPage, wrapper);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "并没有该结果");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 删除
    @GetMapping("/delete/{id}")
    public Vo<HotOrders> delete(@PathVariable("id") String id){
        UpdateWrapper<HotOrders> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = ordersService.remove(wrapper);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }
    // 根据个人信息查询订单
    @GetMapping("/queryOne/{id}")
    public Vo<HotOrders> queryOne(@PathVariable("id") String uid ){
        LambdaQueryWrapper<HotOrders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotOrders::getUId,uid);
        List<HotOrders> list = ordersService.list(wrapper);
        if (list.size() > 0){
            return new Vo<>(true,list);
        }else {
            return new Vo<>(false,"目前没有订单!");
        }
    }

    // 新增订单
    @PostMapping("/insert")
    public Vo<HotOrders> insert(@RequestBody HotOrders orders){
        HotUser user = userService.getById(orders.getUId());
        System.out.println(user);
        if (user == null){
            return new Vo<>(false,"用户没有登录，或者是伪造的id");
        }
        orders.setUName(user.getName());
        boolean b = ordersService.save(orders);
        if (b){
            return new Vo<>(b,"购买成功！");
        }else {
            return new Vo<>(b,"购买失败，请稍后重试！");
        }
    }

}
